﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Meeting.Model;
using Newtonsoft.Json;
using Meeting.Public;

namespace Meeting.Web.Admin.MeetingStand
{
    /// <summary>
    /// StandMeetingOperation 的摘要说明
    /// </summary>
    public class StandMeetingOperation : IHttpHandler
    {

        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 4;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";

            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);
            methodName = context.Request.Params["method"];
            switch (methodName)
            {
                case "SaveStandMeeting":
                    SaveStandMeeting(context);
                    break;
                case "GetStandMeeting":
                    GetStandMeeting(context);
                    break;
                case "DeleteStandMeeting":
                    DeleteStandMeeting(context);
                    break;
                case "GetMeetingById":
                    GetMeetingById(context);
                    break;
                case "GetMeetingEmployeeById":
                    GetMeetingEmployeeById(context);
                    break;
                case "GetMeetingCategory":
                    GetMeetingCategory(context);
                    break;
            }
        }
        BLL.StandMeeting bll = new BLL.StandMeeting();
        //获取OAJson
        private void SaveStandMeeting(HttpContext context)
        {
            Model.StandMeeting model = new Model.StandMeeting();
            string id = context.Request["Id"];
            if (string.IsNullOrEmpty(id))
                model.Id = Guid.Empty;
            else
                model.Id = Guid.Parse(context.Request["Id"]);
            model.Name = CommonFunction.removeAttackOfSql(context.Request["Name"]);
            model.Comment = CommonFunction.removeAttackOfSql(context.Request["Comment"]);
            model.IsActive = context.Request["IsActive"] == "1";
            model.CategoryId =CommonFunction.varToIntDef(context.Request["CategoryId"],0);
            string selEmployee = context.Request["selEmployee"]; 

            JsonResponse jr = new JsonResponse(); 
            if (bll.SaveStandMeeting(model, selEmployee))
            {
                jr.success = true;
            }
            else
            {
                jr.success = false;
            }
            jr.Message = bll.errorString;//true 返回Guid false 返回错误信息
            context.Response.Write(JsonConvert.SerializeObject(jr));
        }
        //
        private void GetStandMeeting(HttpContext context)
        {
            string Name = CommonFunction.removeAttackOfSql(CommonFunction.VarToStr(context.Request["Name"]));
            string sF = "";
            if (Name != "") sF = " Name Like '" + Name + "%' "; 
            context.Response.Write(bll.GetStandMeeting(nPageIndex, nPageSize, sF));
        }
        //
        private void GetMeetingById(HttpContext context)
        {
            string id = context.Request["Id"]; 
            context.Response.Write(bll.GetMeetingById(id,""));
        }
        private void GetMeetingEmployeeById(HttpContext context)
        {
            string id = context.Request["Id"]; 
            context.Response.Write(bll.GetMeetingEmployeeById(id));
        }

        //删除
        private void DeleteStandMeeting(HttpContext context)
        {
            string Id = CommonFunction.VarToStr(context.Request["Id"]);
            JsonResponse jr = new JsonResponse();
            BLL.StandMeeting bll = new BLL.StandMeeting();
            jr.success = bll.DeleteStandMeeting(Id);
            jr.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(jr));
        }

        //获取会议类型
        public void GetMeetingCategory(HttpContext context)
        {
            string Id = CommonFunction.VarToStr(context.Request["Id"]);
            context.Response.Write(bll.GetMeetingCategory(Id));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}